<?php

class epaywxnativePayment extends BasePayment {


    function get_payform($order_info) {
        $order_info=$this->get_order_info($order_info);
        
        if(!$order_info){
            return false;
        }
        require_once(ROOT_PATH ."/includes/payments/epaywxnative/WxPay.NativePay.php");
        define("APPID", Conf::get('payment_wx_appid'));
        define("MCHID", Conf::get('payment_wx_mch_id'));
        define("KEY", Conf::get('payment_wx_key'));
        define("APPSECRET", Conf::get('payment_wx_secret'));


        $notify = new NativePay();
        $input = new WxPayUnifiedOrder();
        $input->SetBody("微信扫码支付"); //商品描述
        $input->SetAttach($order_info['order_sn']); //附加数据
        $input->SetOut_trade_no($order_info['order_sn']); //商户订单号
        $input->SetTotal_fee(intval(bcmul($order_info['money'],100 ,0))); //总金额
        $input->SetTime_start(date("YmdHis", gmtime())); //交易起始时间
        $input->SetTime_expire(date("YmdHis", gmtime() + 3600 * 24)); //交易结束时间
        $input->SetGoods_tag("微信扫码支付"); //商品标记
        $input->SetNotify_url(site_url(). "/notifywx.php");
        $input->SetTrade_type("NATIVE"); //交易类型
        $input->SetProduct_id($order_info['order_sn']); //商品ID
        $result = $notify->GetPayUrl($input);
        $result['order_sn'] = $order_info['order_sn'];
//        $url2 = $result["code_url"];
        return $result;
    }

    function createLinkstring($para) {
        $arg = "";
        while (list ($key, $val) = each($para)) {
            $arg.=$key . "=" . $val . "&";
        }
        //去掉最后一个&字符
        $arg = substr($arg, 0, count($arg) - 2);

        //如果存在转义字符，那么去掉转义
        if (get_magic_quotes_gpc()) {
            $arg = stripslashes($arg);
        }

        return $arg;
    }

    function verify_notify() {
        require_once (ROOT_PATH ."/includes/payments/epaywxnative/lib/WxPay.Api.php");
        require_once (ROOT_PATH .'/includes/payments/epaywxnative/lib/WxPay.Notify.php');


        define("APPID", Conf::get('payment_wx_appid'));
        define("MCHID", Conf::get('payment_wx_mch_id'));
        define("KEY", Conf::get('payment_wx_key'));
        define("APPSECRET", Conf::get('payment_wx_secret'));

        $notify = new WxPayNotify();
        $notify->Handle(false);
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $data = $notify->FromXml($xml);
        //file_put_contents("1.txt",  serialize($data));


        if (!array_key_exists("transaction_id", $data)) {
            $verify_notify = false;
        } else {
            $transaction_id = $data['transaction_id'];
            $input = new WxPayOrderQuery();
            $input->SetTransaction_id($transaction_id);
            $result = WxPayApi::orderQuery($input);
            if (array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
                $verify_notify = TRUE;
            } else {
                $verify_notify = false;
            }
        }
        if ($verify_notify) {
            $notify_result = array(
                'out_trade_no' => $data["out_trade_no"], #商户订单号
                'trade_no' => $data['transaction_id'], #交易凭据单号
                'total_fee' => $data["total_fee"] / 100, #涉及金额
                'trade_status' => '1',
            );
        } else {
            $notify_result = array(
                'trade_status' => '0',
            );
        }

//        file_put_contents(ROOT_PATH ."/includes/payments/epaywxnative/test.php", $this->createLinkstring($data) . $this->createLinkstring($notify_result) . '----' . $verify_notify . '----2222');
        
        return $notify_result;
    }

}
?>
